package com.haidechizi.sort;

import java.util.Arrays;

/**
 * @author: wangdejun
 * @create: 2020-04-14 10:09
 * 时间复杂度：O(n^2)
 * 空间复杂度：O(1)
 **/
public class InsertSort {

    public static void main(String[] args) {
        int[] array = new int[]{9, 3, 17, 8, 5, 6, 7, 3, 5, 6, 2, 1, 33};
        insertSort(array);
        System.out.println(Arrays.toString(array));

    }

    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            int j;
            for (j = i - 1; j >= 0 && array[j] > temp; j--) {
                array[j + 1] = array[j];
            }
            array[j + 1] = temp;
        }
    }
}
